<template>
    <!-- 渲染标签 -->
    <el-popover v-if="props.isTags" :visible="visible" placement="top-start" popper-class="render-tootip" width="auto" :show-after="800" >
        <template #reference>
            <div class="keyword-tags-list" @mouseenter="onMouseenter" @mouseleave="visible = false">
                <el-tag v-for="(item, index) in value" :key="index">{{ item }}</el-tag>
            </div>
        </template>
        <div v-if="value.length" class="tags-list">
            <el-tag v-for="(item, index) in value" :key="index">{{ item }}</el-tag>
        </div>
        <div v-else>--</div>
    </el-popover>
    <!-- 渲染多行文本 -->
    <el-tooltip v-else :visible="visible" :content="props.value" :effect="props.effect"
        popper-class="render-tootip" placement="top" >
        <template v-if="slots.default" #content>
            <slot></slot>
        </template>
        <div class="render-overflow-tootip" :style="{ '--clamp': props.row }" @mouseenter="onMouseenter"
            @mouseleave="onMouseleave">
            <slot v-if="slots.default"></slot>
            <template v-else>{{ props.value ?? '--' }}</template>
        </div>
    </el-tooltip>
</template>

<script lang="ts" setup>
import { ref, useSlots } from "vue";

// 用来判读是否填写了默认的插槽
const slots = useSlots();

const props = defineProps({
    value: {
        type: [String, Array],
    },
    effect: {
        type: String,
        default: "dark"
    },
    /** 换行的行数 */
    row: {
        type: Number,
        default: 2
    },
    isTags: {
        type: Boolean,
        default: false
    }
});

const visible = ref(false);

let timeId = null
const onMouseenter = (e) => {
    const div = e.target as HTMLDivElement;
    if (div.clientHeight < div.scrollHeight || div.clientWidth < div.scrollWidth) {
        timeId = setTimeout(() => {
            visible.value = true
        }, 800)
    }
}

const onMouseleave = () => {
    timeId && clearTimeout(timeId)
    visible.value = false
}
</script>
<style lang="scss" scoped>
.render-overflow-tootip {
    width: 100%;
    word-break: break-all;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: var(--clamp);
    overflow: hidden;
}


.keyword-tags-list {
    cursor: pointer;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

:deep(.el-tag) {
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: inline-block;
    text-align: left;
    vertical-align: middle;
    line-height: 24px;
    margin-right: 6px;
}


.tags-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
</style>
